package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwLyqsclqksmDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwLyqsclqksm;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwLyqsclqksmService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwLyqsclqksmForm;

/*
 * 陆域生活垃圾清扫车辆情况service实现类
 */
@Service
@Transactional
public class YwHwLyqsclqksmServiceImpl  implements IYwHwLyqsclqksmService{
	@Autowired
	private YwHwLyqsclqksmDao ywHwLyqsclqksmDao;
	
	/**
	 * 获取form
	 */
	public YwHwLyqsclqksmForm getForm(Long Id) {
		YwHwLyqsclqksm entity=ywHwLyqsclqksmDao.get(Id);
		YwHwLyqsclqksmForm form=new DozerBeanMapper().map(entity,YwHwLyqsclqksmForm.class);
		return form;
	}

	public List<YwHwLyqsclqksmForm> getListByQyId(Long qyId) {
		List<YwHwLyqsclqksm> list=this.getListByQyid(qyId);
		List<YwHwLyqsclqksmForm> forms=new ArrayList<YwHwLyqsclqksmForm>();
		for(YwHwLyqsclqksm entity:list){
			YwHwLyqsclqksmForm form=new DozerBeanMapper().map(entity,YwHwLyqsclqksmForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据ywid查询数据
	 * @param QYId
	 * @return
	 */
	private List<YwHwLyqsclqksm> getListByQyid(Long qyId){
		String sql=" from "+YwHwLyqsclqksm.class.getName()+" where  qyid="+qyId;
		List<YwHwLyqsclqksm> list=this.ywHwLyqsclqksmDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(YwHwLyqsclqksmForm form) {
		YwHwLyqsclqksm entity=new DozerBeanMapper().map(form,YwHwLyqsclqksm.class);
		this.ywHwLyqsclqksmDao.save(entity);
	}

	public void SaveOrUpdate(YwHwLyqsclqksm entity) {
		this.ywHwLyqsclqksmDao.save(entity);
	}

	public void parseYwHwLyqsclqksm(HSSFSheet hssfsheet,Long qyid,Long ywid, String templateCode,int sheetnum){
	
		String hql = "from YwHwLyqsclqksm t where t.ywid = '"+ywid+"' and t.tplcode= '"+templateCode+"' and sheetnum='"+sheetnum+"'";
		List<YwHwLyqsclqksm> list = new ArrayList<YwHwLyqsclqksm>();
		list = this.ywHwLyqsclqksmDao.find(hql, list);
		for(int i=0; i<list.size();i++){
			this.ywHwLyqsclqksmDao.delete(list.get(i));
		}
		try {
	
			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行
			for (int i = 2; i < hssfsheet.getPhysicalNumberOfRows(); i++) {
				String xh;
				String hp;
				String xxzyjly;
				String xmmc;
				String lx ="";
				hssfrow = hssfsheet.getRow(i);
				
				// 判断是否还存在需要导入的数据
				if (hssfrow != null) {
					if (hssfrow.getCell((short) 2) == null) {
						xh = "";
					} else if (hssfrow.getCell((short) 2).getCellType() == 0) {
						xh = new Double(hssfrow.getCell((short) 2).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						xh = hssfrow.getCell((short) 2).getStringCellValue().trim();
					}
					if (hssfrow.getCell((short) 3) == null) {
						hp = "";
					} else if (hssfrow.getCell((short) 3).getCellType() == 0) {
						hp = new Double(hssfrow.getCell((short) 3).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						hp = hssfrow.getCell((short) 3).getStringCellValue().trim();
					}
					if (hssfrow.getCell((short) 4) == null) {
						xxzyjly = "";
					} else if (hssfrow.getCell((short) 4).getCellType() == 0) {
						xxzyjly = new Double(hssfrow.getCell((short) 4).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						xxzyjly = hssfrow.getCell((short) 4).getStringCellValue().trim();
					}
					if (hssfrow.getCell((short) 5) == null) {
						xmmc = "";
					} else if (hssfrow.getCell((short) 5).getCellType() == 0) {
						xmmc = new Double(hssfrow.getCell((short) 5).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						xmmc = hssfrow.getCell((short) 5).getStringCellValue().trim();
					}
					if(StringUtils.isEmpty(hp)){
					}else{
						if(i<=6 && hssfsheet.getPhysicalNumberOfRows()>=6){//获取类型
							lx=  hssfsheet.getRow(2).getCell((short) 1).getStringCellValue().trim();
						}else if(i<=11 && hssfsheet.getPhysicalNumberOfRows()>=11){
							lx= hssfsheet.getRow(7).getCell((short) 1).getStringCellValue().trim();
						}else if(i<=15 && hssfsheet.getPhysicalNumberOfRows()>=15){
							lx= hssfsheet.getRow(13).getCell((short) 1).getStringCellValue().trim();
						}else if(i<=20 && hssfsheet.getPhysicalNumberOfRows()>=20){
							lx= hssfsheet.getRow(17).getCell((short) 1).getStringCellValue().trim();
						}
						YwHwLyqsclqksm ywHwLyqsclqksm = new YwHwLyqsclqksm();
						ywHwLyqsclqksm.setYwid(ywid);
						ywHwLyqsclqksm.setSheetnum(String.valueOf(sheetnum));
						ywHwLyqsclqksm.setHp(hp);
						ywHwLyqsclqksm.setLx(lx);
						ywHwLyqsclqksm.setQyid(qyid);
						ywHwLyqsclqksm.setXh(xh);
						ywHwLyqsclqksm.setXmmc(xmmc);
						ywHwLyqsclqksm.setXxzyjly(xxzyjly);
						ywHwLyqsclqksm.setTplcode(templateCode);
						this.SaveOrUpdate(ywHwLyqsclqksm);
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * /获取车辆经营的号码
	 * @param id
	 * @param tempcode 业务编码
	 * @return
	 */
	public List<YwHwLyqsclqksm> getCheckCar(Long id, String tempcode) {
		String hql=" from "+YwHwLyqsclqksm.class.getName()+" where ywid="+id+
					" and tplcode='"+tempcode+"'";
		return this.ywHwLyqsclqksmDao.find(hql);
	}

	
}
